﻿using UnityEngine;
using System.Collections;

//For use in state models. Allows for read-only copies of instance data.
public abstract class ReadOnlyable {
	private bool readOnly = false;
	
	//Generates a deep clone of the object with readOnly set to false.
	public ReadOnlyable CloneWritable ()
	{
		ReadOnlyable writable = GetNewInstance();
		CopyTo(writable);
		return writable;
	}

	protected abstract ReadOnlyable GetNewInstance ();
	
	public abstract void CopyTo (ReadOnlyable target);
	
	protected void LogErrorWriteToReadOnly ()
	{
		Debug.LogError("Cannot modify a state with a readOnly property of '" + readOnly + "'.");
	}
	
	protected void LogErrorReadFromReadOnly ()
	{
		Debug.LogError("Cannot read nested objects from a state with a readOnly property of '" + readOnly + "'.");
	}
	
	//Once readyOnly is set to true, it cannot be reset.
	public bool ReadOnly
	{
		get
		{
			return readOnly;
		}
		set
		{
			if (ReadOnly)
			{
				LogErrorWriteToReadOnly();
				return;
			}
			readOnly = value;
		}
	}
}
